2
2
.
.
3
3
.
.
4
4
E
E
n
n
u
u
m
m
e
e
r
r
a
a
t
t
o
o
r
r
s
s
-
-
V
V
a
a
l
l
u
u
e
e
I
I
n
n
f
f
o
o
Think of Value Enumerator as a subset of discrete read only values of certain Data Type: String, Int, Float.
So instead of being able to assign any Int Value to a Variable, you choose to be able to assign only one of the predefined
Int Values from a subset of Values, by making Variable accept specific Enumerator Data Type.
Additionally each Value has Name/Alias which
● allows you to more easily remember values and their meaning
● allows you to avoid changing code when the values change
● allows autocomplete feature to list available names/values
● prevents you from choosing unsupported values
Values must be of following data types and if Value is not explicitly given it is auto generated based on following rules
● String (value is automatically assigned by making it the same as alias itself)
● Int (value is automatically assigned by increasing by one value for previous alias)
● Float (value is automatically assigned by increasing by one value for previous alias)
Initializer is used to define default Alias when none is specified during assignment to Variable Errors().
V
V
a
a
l
l
u
u
e
e
E
E
n
n
u
u
m
m
e
e
r
r
a
a
t
t
o
o
r
r
S
S
y
y
n
n
t
t
a
a
x
x
Value Enumerator is declared by
● using Keyword enum
● followed by Name Errors
● followed by Data Type Int
● followed by Values & their Aliases case Error1 = 10
Value Enumerator Syntax
//DECLARE VALUE ENUMERATOR.
enum Errors : Int { //Eumerator will hold values of Int Data Type
//DECLARE ALIASES.
case Error1 = 10 //Declare aliases and their values.
case Error2 = 20 //Values must be of declared Data Type Int
case Error3 = 30 //Values and aliases must be unique.
case Error4 //Value is set to 31.
case Error5 = 50 //Value is set to 50.
case Error6 //Value is set to 11.
//DECLARE INITIALIZER.
init() { self = .Error1 } //Specifies default Template.
}
//DECLARE VALUE ENUMERATOR VARIABLE.
var connectionError1 : Errors = Errors.Error5 //Variable can store subset of Int values
var connectionError2 : Errors = Errors() //Default Alias: Errors.Error1
//DISPLAY VARIABLE.
print(connectionError1) //Error5
print(connectionError1.rawValue) //31
print(connectionError2) //Error1